package com.hyphenate.easeui.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.controller.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.model.dao.UserIconHelper;
import com.hyphenate.easeui.model.dao.UserIconMap;

public class EaseUserUtils {

    static EaseUserProfileProvider userProvider;

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * 根据username获取相应user
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username){
        if(userProvider != null)
            return userProvider.getUser(username);
        return null;
    }

    /**
     * 设置用户头像
     * @param username
     */
    public static void setUserAvatar(final Context context, String username, final ImageView imageView){
    /*	EaseUser user = getUserInfo(username);
        if(user != null){
            try {
                int avatarResId = Integer.parseInt(user.getAvatar());
                Glide.with(context).load(avatarResId).into(imageView);
            } catch (Exception e) {
                //正常的string路径
                UserIconMap map = UserIconHelper.getUserIconBy(context, username);
                if (map != null && TextUtils.isEmpty(map.getAvatar())) {
                    Glide.with(context).load(map.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ease_default_avatar).into(imageView);
                } else {
                    Glide.with(context).load(R.drawable.ease_default_avatar).into(imageView);
                }
            }
        }else{
            Glide.with(context).load(R.drawable.ease_default_avatar).into(imageView);
        }*/

        UserIconMap map = UserIconHelper.getUserIconBy(context, username);
        if (map != null && !TextUtils.isEmpty(map.getAvatar())) {
//            Glide.with(context).load(map.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ease_default_avatar).into(imageView);

            Glide.with(context).load(map.getAvatar()).asBitmap().centerCrop().into(getBitmapImageViewTarget(context, imageView));

        } else {

//            Glide.with(context).load(R.drawable.ease_default_avatar).into(imageView);
            Glide.with(context).load(R.drawable.ease_default_avatar).asBitmap().centerCrop().into(getBitmapImageViewTarget(context, imageView));
        }
        imageView.setBackgroundColor(Color.parseColor("#00000000"));
    }

    private static BitmapImageViewTarget getBitmapImageViewTarget(final Context context, final ImageView imageView) {
        return new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        };
    }

    /**
     * 设置用户昵称
     */
    public static void setUserNick(Context context, String username,TextView textView){
        if(textView != null){
        	/*EaseUser user = getUserInfo(username);
        	if(user != null && user.getNick() != null){
        		textView.setText(user.getNick());
        	}else{
        		textView.setText(username);
        	}*/

            UserIconMap map = UserIconHelper.getUserIconBy(context, username);
            if (map != null && !TextUtils.isEmpty(map.getNickName())) {
                textView.setText(map.getNickName());
            } else {
                textView.setText(username);
            }
        }
    }

}
